package com.zhc.dejingapi.utils.common;

import com.google.gson.Gson;
import com.zhc.dejingapi.pojo.param.QingCommonBackResult;
import com.zhc.dejingapi.pojo.qingflow.QingflowApiInfo;
import com.zhc.dejingapi.pojo.qingflow.appdata.adddata.QingAddDataResultMain;
import com.zhc.dejingapi.pojo.qingflow.appdata.editdata.*;
import com.zhc.dejingapi.pojo.qingflow.queryresult.QingQueryActionResultBase;
import com.zhc.dejingapi.utils.qing.QingflowAPIUtils;
import io.swagger.models.auth.In;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CommomUpdAIMFieldValueUtils {

    public static String getUpdAIMFieldValueNoCondtion(JdbcTemplate mySqlQFlowJdbcTemplate,
                                                       Long SourceForm_Id,
                                                       String sourceStaticType,
                                                       Boolean bFetchAll,
                                                       List<String> listApplyId,
                                                       Long strSouceQueId,
                                                       Integer strAIMQueType
    )throws Exception
    {


//        if(!bFetchAll){
//            if (listApplyId==null||listApplyId.isEmpty()){
//                throw new Exception("未设置查询条件！");
//            }
//        }
        String strSql="SELECT que_id,que_type from question where form_id=? and que_title=?";

        //Map<String,Object> mapResult=mySqlQFlowJdbcTemplate.queryForMap(strSql,Form_Id,Que_Title);
        //if(mapResult==null||mapResult.isEmpty()){
        //    throw new Exception("当前字段["+Que_Title+"]不存在！");
        //}

        //Object objValue= mapResult.get("que_id");
        //String strQueId=objValue==null? "":objValue.toString();

        //if(strQueId==null||strQueId.isEmpty()){
        //    throw new Exception("当前字段["+Que_Title+"]不存在！");
        //}
        //objValue= mapResult.get("que_type");
        //String strQueType=objValue==null? "":objValue.toString();

        if(!bFetchAll){
            if(listApplyId==null||listApplyId.isEmpty()){
                if(strAIMQueType==8){
                    return "0";
                }
                else{
                    return "";
                }
            }

        }

        strSql="SELECT count(*) from data a \n" +
                " LEFT JOIN apply b on a.apply_id=b.apply_id \n" +
                " where b.being_deleted=0 \n" +
                " and a.form_id=? and a.que_id=?";
        int ihaveCount=mySqlQFlowJdbcTemplate.queryForObject(strSql,Integer.class,SourceForm_Id,strSouceQueId);
        if(ihaveCount==0){
            if(strAIMQueType==8){
                return "0";
            }
            else{
                return "";
            }

        }
        else {
            sourceStaticType = sourceStaticType.toUpperCase();
            if (sourceStaticType.equals("TOP1ASC")) {

                strSql = "SELECT a.value \n" +
                        " from data a \n" +
                        " LEFT JOIN apply b on a.apply_id=b.apply_id \n" +
                        " where b.being_deleted=0 \n" +
                        " and a.form_id=? and a.que_id=?\n" +
                        " ORDER BY a.value LIMIT 1";

            } else if (sourceStaticType.equals("TOP1DESC")) {

                //strSql = "SELECT value from data where form_id=? and que_id=?\n" +
                //        "ORDER BY value desc LIMIT 1";
                strSql = "SELECT a.value \n" +
                        " from data a \n" +
                        " LEFT JOIN apply b on a.apply_id=b.apply_id \n" +
                        " where b.being_deleted=0 \n" +
                        " and a.form_id=? and a.que_id=?\n" +
                        " ORDER BY a.value desc LIMIT 1";
            } else {
                if (strAIMQueType==8) {
                    strSql = "SELECT " + sourceStaticType + "(a.value) \n" +
                            " from data a \n" +
                            " LEFT JOIN apply b on a.apply_id=b.apply_id \n" +
                            " where b.being_deleted=0 \n" +
                            " and a.form_id=? and a.que_id=?  " +
                            "";
                    if (!bFetchAll) {
                        strSql += " and a.apply_id in (" + String.join(",", listApplyId) + ")";
                    }
                    // "and apply_id in ("+String.join(",",listApplyId)+")";
                } else {
                    if (bFetchAll) {
                        strSql = "SELECT value \n" +
                                " from data \n" +
                                " LEFT JOIN apply b on a.apply_id=b.apply_id \n" +
                                " where b.being_deleted=0 \n" +
                                " and a.form_id=? and a.que_id=? \n";
                        strSql += " and a.apply_id in (" + String.join(",", listApplyId) + ")";
                        strSql += "LIMIT 1 ";//字符串默认不允许合计
                    } else {
                        strSql = "SELECT a.value \n" +
                                " from data  a" +
                                " LEFT JOIN apply b on a.apply_id=b.apply_id \n" +
                                " where b.being_deleted=0 \n" +
                                " and a.form_id=? and a.que_id=? \n" +
                                "   \n" +
                                "LIMIT 1 ";//字符串默认不允许合计
                    }


                }
            }

            strSql=mySqlQFlowJdbcTemplate.queryForObject(strSql,String.class,SourceForm_Id,strSouceQueId);
        }

        return strSql;
    }

    public static QingCommonBackResult UpDataFieldValue(QingflowApiInfo qqingflowApiInfo,
                                                 String appKey,
                                                 String strQingApplyId,
                                                 Map<Long,String> mapFieldValue)
    {
        QingCommonBackResult commonBackResult=new QingCommonBackResult();
        boolean isSuccess=false;
        try{
            //String strAccessToken=accessTokenUtils.getAccessToken(qqingflowApiInfo);

            QingEditDataModel qingEditDataModel =new QingEditDataModel();

            List<QingEditDataFieldModel> listFieldModels=new ArrayList<>();

            for (Long strQueId:mapFieldValue.keySet()) {
                //QingAppfieldsEntity sourceEntity=listFields.get(i);

                QingEditDataFieldModel addDataFieldModel = new QingEditDataFieldModel();


                QingEditDataFieldValueModel addDataFieldValueModel = new QingEditDataFieldValueModel();

                addDataFieldValueModel.setValue(mapFieldValue.get(strQueId));
                List<QingEditDataFieldValueModel> listValues = new ArrayList<>();
                listValues.add(addDataFieldValueModel);
                addDataFieldModel.setQueId(strQueId);
                addDataFieldModel.setValues(listValues);

                listFieldModels.add(addDataFieldModel);
            }


            qingEditDataModel.setAnswers(listFieldModels);


            //转字符串
            //String strJson= JSON.toJSONString(qingEditDataModel);
            Gson gson = new Gson();
            String strJson=gson.toJson(qingEditDataModel);

            //获取返回结果
            QingAddDataResultMain resultMain= QingflowAPIUtils.UpdDataToQing(qqingflowApiInfo,appKey,strJson,strQingApplyId);

            if (resultMain==null){

                commonBackResult.setErrCode(-10015);
                commonBackResult.setErrMsg("服务器返回内容为空！");

            }
            else{
                if (resultMain.getErrCode()==0){
                    commonBackResult.setErrCode(resultMain.getErrCode());
                    commonBackResult.setErrMsg("提交成功！");
                    commonBackResult.setBackValue(resultMain.getResult().getRequestId());

                    //获取查询值的结果
                    String strRequestId=resultMain.getResult().getRequestId();
                    try{
                        Thread.sleep(500);
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }
                    QingQueryActionResultBase actionResultBase= QingflowAPIUtils.GetRequestResult(qqingflowApiInfo,strRequestId);
                    if (actionResultBase==null){
                        commonBackResult.setErrCode(-10066);
                        commonBackResult.setErrMsg("提交成功：获取提交结果的内容为空！");

                    }
                    else{
                        if (actionResultBase.getErrorCode()==0){

                            commonBackResult.setErrCode(0);
                            commonBackResult.setErrMsg("提交成功！");
                            String strValue=actionResultBase.getResult();
                            if (strValue==null||strValue.isEmpty()){

                            }
                            else{
                                Long lValue=Long.parseLong(actionResultBase.getResult());
                                commonBackResult.setBackLongValue(lValue);

                            }

                        }
                        else{

                            commonBackResult.setErrCode(actionResultBase.getErrorCode());
                            commonBackResult.setErrMsg(actionResultBase.getMessage());

                        }
                    }



                }
                else{
                    commonBackResult.setErrCode(resultMain.getErrCode());
                    commonBackResult.setErrMsg(resultMain.getErrMsg());

                }

            }

        }
        catch (Exception e){
            e.printStackTrace();
            //throw  e;
            commonBackResult.setErrCode(-10025);
            commonBackResult.setErrMsg("出错:"+e.getMessage()+"\n"+e.getStackTrace());
        }
        return commonBackResult;


    }


    public static QingCommonBackResult UpDataFieldId(QingflowApiInfo qqingflowApiInfo,
                                                        String appKey,
                                                        String strQingApplyId,
                                                        Map<Long,String> mapFieldValue)
    {
        QingCommonBackResult commonBackResult=new QingCommonBackResult();
        boolean isSuccess=false;
        try{
            //String strAccessToken=accessTokenUtils.getAccessToken(qqingflowApiInfo);

            QingEditDataIdModel qingEditDataModel =new QingEditDataIdModel();

            List<QingEditDataIdFieldModel> listFieldModels=new ArrayList<>();

            for (Long strQueId:mapFieldValue.keySet()) {
                //QingAppfieldsEntity sourceEntity=listFields.get(i);

                QingEditDataIdFieldModel addDataFieldModel = new QingEditDataIdFieldModel();


                QingEditDataIdFieldValueModel addDataFieldValueModel = new QingEditDataIdFieldValueModel();

                addDataFieldValueModel.setId(mapFieldValue.get(strQueId));
                List<QingEditDataIdFieldValueModel> listValues = new ArrayList<>();
                listValues.add(addDataFieldValueModel);
                addDataFieldModel.setQueId(strQueId);
                addDataFieldModel.setValues(listValues);

                listFieldModels.add(addDataFieldModel);
            }


            qingEditDataModel.setAnswers(listFieldModels);


            //转字符串
            //String strJson= JSON.toJSONString(qingEditDataModel);
            Gson gson = new Gson();
            String strJson=gson.toJson(qingEditDataModel);

            //获取返回结果
            QingAddDataResultMain resultMain= QingflowAPIUtils.UpdDataToQing(qqingflowApiInfo,appKey,strJson,strQingApplyId);

            if (resultMain==null){

                commonBackResult.setErrCode(-10015);
                commonBackResult.setErrMsg("服务器返回内容为空！");

            }
            else{
                if (resultMain.getErrCode()==0){
                    commonBackResult.setErrCode(resultMain.getErrCode());
                    commonBackResult.setErrMsg("提交成功！");
                    commonBackResult.setBackValue(resultMain.getResult().getRequestId());

                    //获取查询值的结果
                    String strRequestId=resultMain.getResult().getRequestId();
                    try{
                        Thread.sleep(500);
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }
                    QingQueryActionResultBase actionResultBase= QingflowAPIUtils.GetRequestResult(qqingflowApiInfo,strRequestId);
                    if (actionResultBase==null){
                        commonBackResult.setErrCode(-10066);
                        commonBackResult.setErrMsg("提交成功：获取提交结果的内容为空！");

                    }
                    else{
                        if (actionResultBase.getErrorCode()==0){

                            commonBackResult.setErrCode(0);
                            commonBackResult.setErrMsg("提交成功！");
                            String strValue=actionResultBase.getResult();
                            if (strValue==null||strValue.isEmpty()){

                            }
                            else{
                                Long lValue=Long.parseLong(actionResultBase.getResult());
                                commonBackResult.setBackLongValue(lValue);

                            }

                        }
                        else{

                            commonBackResult.setErrCode(actionResultBase.getErrorCode());
                            commonBackResult.setErrMsg(actionResultBase.getMessage());

                        }
                    }



                }
                else{
                    commonBackResult.setErrCode(resultMain.getErrCode());
                    commonBackResult.setErrMsg(resultMain.getErrMsg());

                }

            }

        }
        catch (Exception e){
            e.printStackTrace();
            //throw  e;
            commonBackResult.setErrCode(-10025);
            commonBackResult.setErrMsg("出错:"+e.getMessage()+"\n"+e.getStackTrace());
        }
        return commonBackResult;


    }

}
